package edu.whut.mall.common.service.impl;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import edu.whut.mall.common.service.IFdfsService;
import edu.whut.mall.common.constant.FileConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;


/**
 * Author: smq
 * Unit: D9lab
 * Date: 2021-06-26 16:15
 */
@Service
@Slf4j
public class FdfsServiceImpl implements IFdfsService {

    @Resource
    private FastFileStorageClient fastFileStorageClient;

//    @Resource
//    private FileMapper fileMapper;

    public static String getFileFullPath(String fileStorePath) {
        return FileConstant.fastDfsHost + fileStorePath;
    }


    @Override
    public String upload(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        log.info("上传文件：{}", fileName);
        assert fileName != null;
        String fileExtName = fileName.substring(fileName.lastIndexOf(".") + 1);
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), fileExtName, null);
        String relativeAddress = storePath.getFullPath();
        String url = getFileFullPath(relativeAddress);
        if (url.startsWith("https://47.96.156.152")) {
            url = url.replace("https://47.96.156.152", "http://d9labmall.top");
        }
        return url;
    }
}
